using System;
using PluginFrame;

namespace PcrDataAnalysis
{
	public class RealTimeAutoBaseLineNormalization : AutoBaseLineNormalization, IBaseLineNormalization
	{
		public RealTimeAutoBaseLineNormalization(IApplication app)
			: base(app)
		{
			mRotateCurve = false;
		}

		public new void Execute(IPcrCell pcrCell, IAbsoluteAnalysisParameterConfiguration analysisOption, int channelIndex, bool isAnalyzing)
		{
			analysisOption.IsDigitalFilter = false;
			mStartBaseLine = analysisOption.StartBaseline;
			mStopBaseLine = analysisOption.StopBaseline;
			pcrCell.CheckRawData(channelIndex);
			double[] channelRawData = pcrCell.GetChannelRawData(channelIndex, true);
			PreAnalysisNormalization(pcrCell, channelIndex, mStartBaseLine, mStopBaseLine, channelRawData);
			RealTimeCurveRotate(pcrCell, channelIndex, pcrCell.GetChannelRawData(channelIndex, true));
			pcrCell.InitializeAmplificationCurveSeries(channelIndex);
			for (int i = 1; i <= channelRawData.Length; i++)
			{
				double normalLightData = pcrCell.GetNormalLightData(channelIndex, i);
				pcrCell.SetCurveLightData(channelIndex, i, normalLightData);
			}
		}

		private void RealTimeCurveRotate(IPcrCell pcrCell, int channelIndex, double[] channelData)
		{
			mAutoOptimizationCycle = pcrCell.GetAutoBaseLineRotationCount(channelIndex);
			double[] channelLightData = pcrCell.GetChannelRawData(channelIndex, true);
			if (mAutoOptimizationCycle > 1)
			{
				int autoOptimizationDefaultStartValue = mAnalysisConfiguration.AutoOptimizationDefaultStartValue;
				double autoOptimizationThreshold = mAnalysisConfiguration.AutoOptimizationThreshold;
				int num = 0;
				for (int i = 1; i <= mAutoOptimizationCycle; i++)
				{
					mStartBaseLine = pcrCell.GetAutoBaseLineStartValue(channelIndex);
					mStopBaseLine = pcrCell.GetAutoBaseLineStopValue(channelIndex);
					double num2 = CalculateBaseLineAverage(channelData, mStartBaseLine, mStopBaseLine);
					autoOptimizationThreshold = Math.Pow(2.0, mAnalysisConfiguration.AutoOptimizationThreshold) * num2;
					for (int j = autoOptimizationDefaultStartValue; j <= channelData.Length; j++)
					{
						if (!double.IsNaN(channelData[j - 1]) && channelData[j - 1] > autoOptimizationThreshold)
						{
							num = j;
							break;
						}
					}
					if (num == 0)
					{
						num = channelData.Length;
					}
					mStopBaseLine = num - 5;
					if (mStopBaseLine <= mAnalysisConfiguration.AutoBaseLineMinStartValue)
					{
						mStopBaseLine = pcrCell.GetAutoBaseLineStopValue(channelIndex);
						mStartBaseLine = pcrCell.GetAutoBaseLineStartValue(channelIndex);
						if (mStopBaseLine > channelData.Length)
						{
							mStopBaseLine = channelData.Length;
						}
						if (mStartBaseLine > channelData.Length)
						{
							mStopBaseLine = channelData.Length;
							mStartBaseLine = mStopBaseLine - mAnalysisConfiguration.AutoBaseLineDefaultInterval;
							if (mStartBaseLine < mAnalysisConfiguration.AutoBaseLineMinStartValue)
							{
								mStartBaseLine = mAnalysisConfiguration.AutoBaseLineMinStartValue;
							}
							if (mStartBaseLine >= mStopBaseLine)
							{
								mStartBaseLine = mStopBaseLine - 1;
							}
						}
					}
					else
					{
						mAnalysisConfiguration.AutoBaseLineDefaultInterval = 8;
						if (mStopBaseLine - mStartBaseLine < mAnalysisConfiguration.AutoBaseLineDefaultInterval)
						{
							mStartBaseLine = mStopBaseLine - mAnalysisConfiguration.AutoBaseLineDefaultInterval;
							if (mStartBaseLine < mAnalysisConfiguration.AutoBaseLineMinStartValue)
							{
								mStartBaseLine = mAnalysisConfiguration.AutoBaseLineMinStartValue;
								if (mStopBaseLine < mAnalysisConfiguration.AutoBaseLineDefaultInterval)
								{
									mStopBaseLine = mAnalysisConfiguration.AutoBaseLineDefaultInterval;
								}
								if (mStopBaseLine > channelData.Length)
								{
									mStopBaseLine = channelData.Length;
								}
							}
						}
					}
					RotateTranlateData(pcrCell, ref channelLightData, channelIndex, mStartBaseLine, mStopBaseLine);
				}
			}
			else
			{
				mStartBaseLine = pcrCell.GetAutoBaseLineStartValue(channelIndex);
				mStopBaseLine = pcrCell.GetAutoBaseLineStopValue(channelIndex);
				RotateTranlateData(pcrCell, ref channelLightData, channelIndex, mStartBaseLine, mStopBaseLine);
			}
			mRotateCurve = false;
			ChannelNormalization(pcrCell, channelIndex, true, mStartBaseLine, mStopBaseLine, channelLightData);
		}
	}
}
